<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				infoCacheKey:"hr-kq-yearLeave-info-cache",
				yStartTimeCache:'hr-kq-yearLeave-yStartTime-cache',
				endTime:"",
				endTime1:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			formSubmit(){
				let _that = this
				if(this.checkedit){
					this.om.formSave('taskLeave/saveEdit',_that.formdata,_that)					
				}else{
					this.om.formSave('taskLeave/save',_that.formdata,_that)					
				}
			},
			getLeaveStatisticsInfo(){
				let _that = this
				_that.om.request(
					"taskLeave/getLeaveStatisticsInfo",{
						startTime:_that.$refs.autoform.formdata.yStartDate,
						userId: _that.$refs.autoform.formdata.infoId
					}
				).then(res=>{
					_that.$refs.autoform.formdata.leaveStatistics = res.value
				})
			},
			onSelectYstart(item,date){
				let _that = this
				_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"startTime", date+' ' + item.attr.startTime)
				_that.$refs.autoform.formdata.yStartDate = date+' ' + item.attr.startTime
				_that.endTime = item.attr.endTime
				_that.endTime1 = item.attr.endTime1
				_that.verifyYearLeave()
			},
			calEndTime(){
				let _that = this
				_that.om.request(
					"ttimeTable/calEndTime",
					{
						startTime: _that.$refs.autoform.formdata.startTime,
						date: _that.$refs.autoform.formdata.leaveDay,
						endTime: _that.endTime,
						endTime1: _that.endTime1
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.endTime = res.value
					}else{
						_that.om.toast(res.msg)
					}
					_that.getLeaveStatisticsInfo()
				})
			},
			verifyYearLeave(){
				let _that = this
				if(!_that.$refs.autoform.formdata.yStartDate){
					return
				}
				_that.om.request(
					"taskLeave/verifyYearLeave",
					{
						leaveClassId: _that.$refs.autoform.formdata.leaveClassId,
						userId: _that.$refs.autoform.formdata.infoId,
						startTime: _that.$refs.autoform.formdata.startTime
					}
				).then(res=>{
					if(res.succeed){
						if(res.value){
							_that.om.toast(res.value)
							_that.$refs.autoform.clear("leaveTimeTitle","yStartTime")
							_that.$refs.autoform.clear("leaveTimeTitle","yStartDate")
							_that.$refs.autoform.clear("leaveTimeTitle","endTime")
						}else{
							_that.calEndTime()
						}
					}
				})
			},
			onInputDay(val){
				let _that = this
				let day = Number(val)
				if(day<0.5){
					_that.$refs.autoform.formdata.leaveDay = 0.5
				}else{
					let num = day%0.5
					if(num != 0){
						_that.$refs.autoform.formdata.leaveDay = day-num
					}
				}
				_that.verifyYearLeave()
			},
			getStartNodeList(){
				let _that = this
				_that.om.request(
					"ttimeTable/getStartNodeList",
					{
						userId: _that.$refs.autoform.formdata.infoId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("leaveTimeTitle","yStartDate","data",configSelectData(res,"name","name",true))
					_that.$refs.autoform.changeEditor("leaveTimeTitle","yStartDate","showDetail1","name")
					_that.$refs.autoform.changeEditor("leaveTimeTitle","yStartDate","showDetail2","startTime")
					
				})
			},
			// verifyYStartTime(){
			// 	let _that = this
			// 	if(!_that.$refs.autoform.formdata.infoId){
			// 		let title = _that.$refs.autoform.getEditorTitle("leavePersonTitle","infoId")
			// 		_that.om.toast("请先选择"+title)
			// 		return false
			// 	}else{
			// 		_that.$refs.autoform.changeEditor("leaveTimeTitle","yStartTime",{
			// 			link:"../../public/search/search",
			// 			"linkParam":{
			// 				other:JSON.stringify({
			// 					userId: _that.$refs.autoform.formdata.infoId
			// 				}),
			// 				namefield:"name", 
			// 				extrafield:"node",
			// 				notefield:"startTime",
			// 				url:'ttimeTable/getStartNodeList',
			// 				initload:true,
			// 				cachename:_that.yStartTimeCache,
			// 				placeholder:"请输入班次、节点、时间"
			// 			}
			// 		})
			// 		return true
			// 	}
			// },
			getListByCode(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:'36924',
						inCode:'36925'
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("leaveTimeTitle","replaceFlagStr",'data',configSelectData(res,'dicname','dicname'))
					_that.$refs.autoform.initSelectIndex("leaveTimeTitle","replaceFlagStr")
				})
			},
			getInfo(userId){
				let _that = this
				_that.om.request(
					"tworkInfo/getInfo",
					{
						userId:userId
					}
				).then(res=>{
					_that.subEditorObj["takeOffice"].workList = res.value
				})
			},
			getUserInfo(userId){
				let _that = this
				_that.om.request(
					"taskLeave/getUserInfo",
					{
						userId:userId
					}
				).then(res=>{
					if(res.value){
						_that.$refs.autoform.formdata.birthdate =  res.value.temployeeResponse.birthdate	
						_that.$refs.autoform.formdata.birthPlace =  res.value.temployeeResponse.birthPlace	
						_that.$refs.autoform.formdata.sexName =  res.value.temployeeResponse.sexName	
						_that.$refs.autoform.formdata.highestEduName = res.value.temployeeResponse.highestEduName	
						_that.$refs.autoform.formdata.mobile=  res.value.temployeeResponse.mobile
						_that.$refs.autoform.formdata.entrydate =  res.value.temployeeResponse.entrydate
						_that.$refs.autoform.formdata.takeTime = res.value.taskLeave.takeTime
					}
				})
			},
			canContinueAskLeave(id){
				let _that = this
				_that.om.request(
					"taskLeave/canContinueAskleave",
					{
						infoId:id
					}
				).then(res=>{
					if(res.succeed && !res.value){
						_that.getUserInfo(id)
						_that.getInfo(id)
						_that.getListByCode()
						if(_that.$refs.autoform.formdata.startTime){
							_that.verifyFormDate(_that.$refs.autoform.formdata.startTime)
						}
					}else{
						_that.om.toast(res.value)
					}
				})
			},
			getList(){
				let _that = this
				_that.om.request(
					"askLeaveDict/getList"
				).then(res=>{
					for(let item of res){
						if(item.id == 7){
							_that.$refs.autoform.formdata.leaveClassId = item.id
							_that.$refs.autoform.formdata.leaveClassName = item.dicname
							_that.$refs.autoform.formdata.salaryRatio = item.ratio
						}
					}
				})
			},
			initLoad(options) {
				let _that = this
				let ajaxArr = []
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('topinfo','zuserId',{
						defaultName:_that.userName,
						defaultValue:_that.userId
					},null, mainData);
					changeEditor("leavePersonTitle","infoId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:urlUtil.findUserAsSameInst().url,
							namefield:urlUtil.findUserAsSameInst().namefield,
							extrafield:urlUtil.findUserAsSameInst().extrafield,
							initload:true,
							searchfield:"q",
							cachename:_that.infoCacheKey,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					if(_that.subEditorObj["takeOffice"]){
						let att = {
							showAdd:false,
							showArrow:false,
							showDel:false
						}
						let configData = _that.subConfigData["takeOffice"]
						if(hasEditor("takeOffice","posName",configData)){
							let title = getEditorTitle("takeOffice","posName",configData)
							att.titleTemplate = title+":<%= posName %>"
						}
						if(hasEditor("takeOffice","salaryPosFlagStr",configData)){
							let title = getEditorTitle("takeOffice","salaryPosFlagStr",configData)
							att.titleTemplate = att.titleTemplate+"<%= salaryPosFlagStr %>"
						}
						if(hasEditor("takeOffice","parentUserName",configData)){
							let title = getEditorTitle("takeOffice","parentUserName",configData)
							att.subtitle1Template = title+":<%= parentUserName %>"
						}
						if(hasEditor("takeOffice","instShort",configData)){
							let title = getEditorTitle("takeOffice","instShort",configData)
							att.subtitle1Template = att.subtitle1Template +'|'+  title +":<%= instShort %>"
						}
						if(hasEditor("takeOffice","parentInst",configData)){
							let title = getEditorTitle("takeOffice","parentInst",configData)
							att.subtitle2Template = title +":<%= parentInst %>"
						}
						if(hasEditor("takeOffice","salaryWay",configData)){
							let title = getEditorTitle("takeOffice","salaryWay",configData)
							att.subtitle2Template = att.subtitle2Template +'|'+ title +":<%= salaryWay %>"
						} 
						changeEditor("takeOffice","workStr",att,mainData)
					}
					_that.$nextTick(function() {
						let editorArr = [{
								checkedit: null,
								editReadonly: null,
								isfull: null,
								max: "",
								min: "",
								prompt: "请输入0.5的倍数",
								nameField: "leaveDay",
								required: true,
								title: "请假天数",
								type: "numberbox",
								valueField: "leaveDay",
								onSelect:'onInputDay'
							},
							{
								editReadonly: null,
								isfull: null,
								max: "",
								min: "",
								nameField: "yStartDate",
								onSelect: "",
								prompt: "请选择",
								required: true,
								title: "开始日期",
								type: "opickerbox",
								valueField: "yStartDate",
								onSelect:'onSelectYstart'
							}
						]
						_that.$refs.autoform.insertAfterEditorArr("leaveTimeTitle", 'salaryRatio',editorArr)
						_that.getList()
						if(!_that.$refs.autoform.formdata.infoId){
							_that.$refs.autoform.formdata.infoId = _that.userId
							_that.$refs.autoform.formdata.infoName = _that.userName
							_that.canContinueAskLeave(_that.userId)
						}
						_that.getStartNodeList()
					})

				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.infoCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.infoId = d.id
							formdata.infoName = d.username
							_that.$refs.autoform.clear("leaveTimeTitle","leaveClassId")
							_that.$refs.autoform.clear("leaveTimeTitle","salaryRatio")
							_that.$refs.autoform.clear("leaveTimeTitle","replaceFlagStr")
							_that.canContinueAskLeave(d.id)
							uni.removeStorage({
								key:_that.infoCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>
